//
//  MoreItemRequest.m
//  SleepingElf
//
//  Created by karong on 2023/10/21.
//

#import "MoreItemRequest.h"

@implementation MoreItemRequest

///白噪音更多
+ (void)loadMoreItemlistWithClassify_id:(NSInteger)classify_id page:(NSInteger)page pageSize:(NSInteger)pageSize success:(ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_HOME_EXCLUSIVE;
    NSMutableDictionary * parameters = [NSMutableDictionary new];
    if (classify_id > 0) {
        parameters[@"classify_id"] = @(classify_id);
    }
    parameters[@"page"] = @(page);
    parameters[@"pagesize"] = @(pageSize);
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSArray * array = [MusicModel mj_objectArrayWithKeyValuesArray:responseDict[@"data"]];
        success(array);
    } failure:failed];
}

#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(ArrayBlock)success failed:(Failure)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.haveMore = YES;
    self.page = 1;
    self.pageSize = 50;
    [MoreItemRequest loadMoreItemlistWithClassify_id:self.classify_id page:self.page pageSize:self.pageSize success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(ArrayBlock)success failed:(Failure)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    
    [MoreItemRequest loadMoreItemlistWithClassify_id:self.classify_id page:self.page pageSize:self.pageSize success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(ArrayBlock)success withArray:(NSArray *)array {
    self.haveMore = !(array.count < self.pageSize);
    self.page++;
    self.loading = NO;
    success(array);
}
@end
